package com.golddaymicromall.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.golddaymicromall.activity.eleaccounts.DianZiZhangHuActivity;
import com.golddaymicromall.activity.jifen.JiFenActivity;
import com.golddaymicromall.activity.leavemsg.LeaveMsgActivity;
import com.golddaymicromall.activity.notice.NoticeActivity;
import com.golddaymicromall.activity.profit.ProfitActivity;
import com.golddaymicromall.activity.recharge.RechargeActivity;
import com.golddaymicromall.activity.salemanager.SaleManagerActivity;
import com.golddaymicromall.activity.setting.about.SettingActivity;
import com.golddaymicromall.activity.storemanager.StoreManagerActivity;
import com.golddaymicromall.base.BaseActivity;
import com.golddaymicromall.bean.PanicBuyingInfo;
import com.golddaymicromall.img.HeadImageLoad;
import com.golddaymicromall.model.HttpModel;
import com.golddaymicromall.util.DateFormatUtils;
import com.golddaymicromall.util.Log;
import com.golddaymicromall.util.RopUtils;
import com.golddaymicromall.util.SPUtils;
import com.golddaymicromall.util.ServerConfig;
import com.golddaymicromall.view.CyclerImg;
import com.google.common.collect.Maps;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.convert.StringConvert;
import com.lzy.okrx.RxAdapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import rx.android.schedulers.AndroidSchedulers;

public class MeActivity extends BaseActivity implements View.OnClickListener {
    private com.golddaymicromall.view.CyclerImg mMyFragment_UserHead;
    private TextView mMyFragment_UserName;
    private TextView MyFragment_SetImg;
    private LinearLayout leaveMsg;
    private ImageView mMyFragment_LoginBtn;
    private LinearLayout mStore_manager;
    private LinearLayout mRecharge;
    private LinearLayout mSale_manager;
    private LinearLayout mProfit;
    private LinearLayout mDianZiZhangHu;
    private LinearLayout mTongzhi;
    private LinearLayout mRVshezhi;
    private LinearLayout mhome;


    private HeadImageLoad headImageLoad;
    private GridView gridView;
    private SimpleAdapter saImageItems;
    private PanicBuyingInfo panicbuyingInfo;
    private ArrayList<HashMap<String, Object>> lstImageItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //透明状态栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        setContentView(R.layout.me_fg);
        headImageLoad = new HeadImageLoad();
    }

    private void initView() {
        mRecharge = (LinearLayout) findViewById(R.id.Recharge);
        mStore_manager = (LinearLayout) findViewById(R.id.store_manager);
        mMyFragment_UserHead = (CyclerImg) findViewById(R.id.MeFragment_UserHead);
        mMyFragment_UserName = (TextView) findViewById(R.id.MyFragment_UserName);
        mMyFragment_LoginBtn = (ImageView) findViewById(R.id.MyFragment_LoginBtn);
        MyFragment_SetImg = (TextView) findViewById(R.id.MyFragment_SetImg);
        mDianZiZhangHu = (LinearLayout) findViewById(R.id.DianZiZhangHu);
        leaveMsg = (LinearLayout) findViewById(R.id.leaveMsg);
        mSale_manager = (LinearLayout) findViewById(R.id.salemanager);
        mProfit = (LinearLayout) findViewById(R.id.profit);
        mTongzhi = (LinearLayout) findViewById(R.id.tongzhi);
        mhome = (LinearLayout) findViewById(R.id.home);


        mRVshezhi = (LinearLayout) findViewById(R.id.RVshezhi);

        gridView = (GridView) findViewById(R.id.gridview);
        mMyFragment_LoginBtn.setOnClickListener(this);
        mhome.setOnClickListener(this);
        mDianZiZhangHu.setOnClickListener(this);
        mRVshezhi.setOnClickListener(this);
        leaveMsg.setOnClickListener(this);
        mRecharge.setOnClickListener(this);
        mStore_manager.setOnClickListener(this);
        MyFragment_SetImg.setOnClickListener(this);
        mSale_manager.setOnClickListener(this);
        mProfit.setOnClickListener(this);
        mTongzhi.setOnClickListener(this);

        //生成动态数组，并且转入数据
        lstImageItem = new ArrayList<HashMap<String, Object>>();
        HashMap<String, Object> map1 = new HashMap<String, Object>();
        map1.put("ItemImage", R.drawable.ic_jinhuo_dianpu);//添加图像资源的ID
        map1.put("ItemText", "店铺管理");
        lstImageItem.add(map1);
        HashMap<String, Object> map2 = new HashMap<String, Object>();
        map2.put("ItemImage", R.drawable.ic_jinhuo_xiaoshou);//添加图像资源的ID
        map2.put("ItemText", "进货管理");
        lstImageItem.add(map2);
        HashMap<String, Object> map3 = new HashMap<String, Object>();
        map3.put("ItemImage", R.drawable.ic_jinhuo_chongzhi);//添加图像资源的ID
        map3.put("ItemText", "充值");
        lstImageItem.add(map3);
        HashMap<String, Object> map4 = new HashMap<String, Object>();
        map4.put("ItemImage", R.drawable.ic_jinhuo_dianzizhanghu);//添加图像资源的ID
        map4.put("ItemText", "电子账户");
        lstImageItem.add(map4);
        if (!"28".equals(SPUtils.get(this, "OrganizationId", "").toString())) {
            HashMap<String, Object> map5 = new HashMap<String, Object>();
            map5.put("ItemImage", R.drawable.ic_jinhuo_shouyi);//添加图像资源的ID
            map5.put("ItemText", "收益汇总");
            lstImageItem.add(map5);
        }
        HashMap<String, Object> map6 = new HashMap<String, Object>();
        map6.put("ItemImage", R.drawable.ic_jinhuo_liuyan1);//添加图像资源的ID
        map6.put("ItemText", "在线咨询");
        lstImageItem.add(map6);
        HashMap<String, Object> map7 = new HashMap<String, Object>();
        map7.put("ItemImage", R.drawable.ic_gonggao);//添加图像资源的ID
        map7.put("ItemText", "通知公告");
        lstImageItem.add(map7);
        HashMap<String, Object> map8 = new HashMap<String, Object>();
        map8.put("ItemImage", R.drawable.jifen);//添加图像资源的ID
        map8.put("ItemText", "积分活动");
        lstImageItem.add(map8);


        //生成适配器的ImageItem <====> 动态数组的元素，两者一一对应
        saImageItems = new SimpleAdapter(this, //没什么解释
                lstImageItem,//数据来源
                R.layout.gridview_item,//night_item的XML实现
                //动态数组与ImageItem对应的子项
                new String[]{"ItemImage", "ItemText"},
                //ImageItem的XML文件里面的一个ImageView,两个TextView ID
                new int[]{R.id.ItemImage, R.id.ItemText});
        //添加并且显示
        gridView.setAdapter(saImageItems);
        //添加消息处理
        gridView.setOnItemClickListener(new ItemClickListener());
    }

    //当AdapterView被单击(触摸屏或者键盘)，则返回的Item单击事件
    class ItemClickListener implements AdapterView.OnItemClickListener {
        public void onItemClick(AdapterView<?> arg0,//The AdapterView where the click happened
                                View arg1,//The view within the AdapterView that was clicked
                                int arg2,//The position of the view in the adapter
                                long arg3//The row id of the item that was clicked
        ) {
            String Id = SPUtils.get(MeActivity.this, "Id", "").toString();
            //在本例中arg2=arg3
            HashMap<String, Object> item = (HashMap<String, Object>) arg0.getItemAtPosition(arg2);
            //显示所选Item的ItemText
            switch ((String) item.get("ItemText")) {
                case "店铺管理":
                    if ("".equals(Id)) {
                        Toast.makeText(MeActivity.this, "您还没有登录，请登录！！！", Toast.LENGTH_SHORT).show();
                        Intent intentStore2Login = new Intent(MeActivity.this, LoginActivity.class);
                        startActivity(intentStore2Login);
                    } else {
                        Intent intent_store = new Intent(MeActivity.this, StoreManagerActivity.class);
                        MeActivity.this.startActivity(intent_store);
                    }
                    break;
                case "进货管理":
                    if ("".equals(Id)) {
                        Toast.makeText(MeActivity.this, "您还没有登录，请登录！！！", Toast.LENGTH_SHORT).show();
                        Intent intentStore2Login = new Intent(MeActivity.this, LoginActivity.class);
                        startActivity(intentStore2Login);
                    } else {
                        Intent intent_sale = new Intent(MeActivity.this, SaleManagerActivity.class);
                        MeActivity.this.startActivity(intent_sale);
                    }
                    break;
                case "充值":
                    if ("".equals(Id)) {
                        Toast.makeText(MeActivity.this, "您还没有登录，请登录！！！", Toast.LENGTH_SHORT).show();
                        Intent intentSet2Login = new Intent(MeActivity.this, LoginActivity.class);
                        startActivity(intentSet2Login);
                    } else {
                        Intent intent_Recharge = new Intent(MeActivity.this, RechargeActivity.class);
                        MeActivity.this.startActivity(intent_Recharge);
                    }
                    break;
                case "电子账户":
                    if ("".equals(Id)) {
                        Toast.makeText(MeActivity.this, "您还没有登录，请登录！！！", Toast.LENGTH_SHORT).show();
                        Intent intentSet2Login = new Intent(MeActivity.this, LoginActivity.class);
                        startActivity(intentSet2Login);
                    } else {
                        Intent intentDianZiZhangHu = new Intent(MeActivity.this, DianZiZhangHuActivity.class);
                        MeActivity.this.startActivity(intentDianZiZhangHu);
                    }
                    break;
                case "收益汇总":
                    if ("".equals(Id)) {
                        Toast.makeText(MeActivity.this, "您还没有登录，请登录！！！", Toast.LENGTH_SHORT).show();
                        Intent intentStore2Login = new Intent(MeActivity.this, LoginActivity.class);
                        startActivity(intentStore2Login);
                    } else {
                        Intent intent_profit = new Intent(MeActivity.this, ProfitActivity.class);
                        MeActivity.this.startActivity(intent_profit);
                    }
                    break;
                case "在线咨询":
                    if ("".equals(Id)) {
                        Toast.makeText(MeActivity.this, "您还没有登录，请登录！！！", Toast.LENGTH_SHORT).show();
                        Intent intentSet2Login = new Intent(MeActivity.this, LoginActivity.class);
                        startActivity(intentSet2Login);
                    } else {
                        Intent intent_LeaveMsg = new Intent(MeActivity.this, LeaveMsgActivity.class);
                        MeActivity.this.startActivity(intent_LeaveMsg);
                    }
                    break;
                case "通知公告":
                    if ("".equals(Id)) {
                        Toast.makeText(MeActivity.this, "您还没有登录，请登录！！！", Toast.LENGTH_SHORT).show();
                        Intent intentSet2Login = new Intent(MeActivity.this, LoginActivity.class);
                        startActivity(intentSet2Login);
                    } else {
                        Intent intent1 = new Intent(MeActivity.this, NoticeActivity.class);
                        startActivity(intent1);
                    }
                    break;
                case "活动专区":
                    if ("".equals(Id)) {
                        Toast.makeText(MeActivity.this, "您还没有登录，请登录！！！", Toast.LENGTH_SHORT).show();
                        Intent intentSet2Login = new Intent(MeActivity.this, LoginActivity.class);
                        startActivity(intentSet2Login);
                    } else {

                        if (panicbuyingInfo.getCode() == 3) {
                            Toast.makeText(MeActivity.this, "该账户未激活，请到首次进货进行激活！！！", Toast.LENGTH_SHORT).show();
                        } else {
                            startActivity(new Intent(MeActivity.this, SpecialColumnActivity.class));
                        }

                    }
                    break;
                case "积分活动":
                    if ("".equals(Id)) {
                        Toast.makeText(MeActivity.this, "您还没有登录，请登录！！！", Toast.LENGTH_SHORT).show();
                        Intent intentSet2Login = new Intent(MeActivity.this, LoginActivity.class);
                        startActivity(intentSet2Login);
                    } else {
                        Intent toJIFEN = new Intent(MeActivity.this, JiFenActivity.class);
                        startActivity(toJIFEN);
                    }
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public void onClick(View v) {
        String Id = SPUtils.get(MeActivity.this, "Id", "").toString();
        switch (v.getId()) {
            case R.id.MyFragment_LoginBtn:
                Intent intent = new Intent(MeActivity.this, LoginActivity.class);
                startActivity(intent);
                break;
            case R.id.RVshezhi:
                if ("".equals(Id)) {
                    Toast.makeText(MeActivity.this, "您还没有登录，请登录！！！", Toast.LENGTH_SHORT).show();
                    Intent intentSet2Login = new Intent(MeActivity.this, LoginActivity.class);
                    startActivity(intentSet2Login);
                } else {
                    Intent intent_Set = new Intent(MeActivity.this, SettingActivity.class);
                    MeActivity.this.startActivity(intent_Set);
                }
                break;
            case R.id.home:
                finish();
                break;
            default:
                break;
        }
    }


    @Override
    public void onResume() {
        super.onResume();
        initView();
        updateUserView();
        String headImgName = SPUtils.get(MeActivity.this, "headImg", "").toString();
        isColumn();
        if (!"".equals(headImgName)) {
            Glide.with(this)
                    .load(HttpModel.HEADIMGURL + headImgName).placeholder(R.drawable.touxiang)
                    .into(mMyFragment_UserHead);
        }
    }

    private void updateUserView() {
        //区分用户是否登录
        String Id = SPUtils.get(MeActivity.this, "Id", "").toString();
        String Username = SPUtils.get(MeActivity.this, "Username", "").toString();
        if ("".equals(Id)) {
            //用户没有登录->隐藏头像以及昵称空间
            mMyFragment_UserHead.setVisibility(View.GONE);
            mMyFragment_UserName.setVisibility(View.GONE);
            mMyFragment_LoginBtn.setVisibility(View.VISIBLE);
        } else {
            //隐藏登录按钮
            mMyFragment_UserHead.setVisibility(View.VISIBLE);
            mMyFragment_UserName.setVisibility(View.VISIBLE);
            mMyFragment_LoginBtn.setVisibility(View.GONE);
            //设置用户名以及用户头像
            mMyFragment_UserName.setText(Username);
        }
    }

    private void isColumn() {
        String Id = SPUtils.get(MeActivity.this, "Id", "").toString();
        Map<String, String> form = Maps.newHashMap();
        form.put("method", "rop.product.panic");
        form.put("appKey", "00001");
        form.put("v", "1.0");
        form.put("format", "json");
        form.put("id", Id);
        String sing = RopUtils.signString(form, "qwertyuiop");
        form.put("sign", sing);
        OkGo.post(new ServerConfig().SERVER)
                .params(form)
                .getCall(StringConvert.create(), RxAdapter.<String>create())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(s -> {
                    Log.e("aaaaa", s);
                    panicbuyingInfo = JSON.parseObject(s, PanicBuyingInfo.class);

                    String Sysdate = DateFormatUtils.dateTostring(panicbuyingInfo.getSysdate());
                    Log.e("Sysdate", Sysdate);
                    String begindate = DateFormatUtils.dateTostring(panicbuyingInfo.getBegin());
                    Log.e("begindate", begindate);
                    String enddate = DateFormatUtils.dateTostring(panicbuyingInfo.getEnd());
                    Log.e("enddate", enddate);
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

                    try {
                        Date Sysdate1 = sdf.parse(Sysdate);
                        Date begindate1 = sdf.parse(begindate);
                        Date enddate1 = sdf.parse(enddate);
                        int a = Sysdate1.compareTo(begindate1);
                        int b = enddate1.compareTo(Sysdate1);
                        if(a >= 0 && b >= 0){
                            HashMap<String, Object> map8 = new HashMap<String, Object>();
                            map8.put("ItemImage", R.drawable.play);//添加图像资源的ID
                            map8.put("ItemText", "活动专区");
                            lstImageItem.add(map8);
                            saImageItems.notifyDataSetChanged();
                        }

                    } catch (ParseException e) {
                        e.printStackTrace();
                    }

                }, throwable -> {
                    throwable.printStackTrace();
                });

    }

}
